package guilayer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import modellayer.KeyPerson;
import modellayer.ZipCode;
import controllerlayer.AppointmentCtrl;
import controllerlayer.KeyPersonCtrl;
import controllerlayer.LeadCtrl;
import controllerlayer.TimestampCtrl;
import controllerlayer.ZipCodeCtrl;

import java.awt.Color;

public class CreateLead extends JFrame
{	
	private JPanel contentPane;
	private JTextField txtLeadEmail;	
	private JTextField txtLeadWebsite;	
	private JComboBox cbKeyPerson;	
	private JLabel lblLblerror;
	private JTextField txtLeadPhone;
	private JTextField txtLeadCompany;
	private JTextField txtLeadName;
	private JTextField txtLeadAddedDate;

	
	/**
	 * Create the frame.
	 */	
	public CreateLead() 
	{
		/**
		 * Setup frame
		 */
		setTitle("Fairway Networks | Opret Lead");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 475, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		getContentPane().setLayout(null);
		contentPane.setLayout(null);
		
		
		/**
		 * Set up JLabels
		 */		
		
		cbKeyPerson = new JComboBox(listOfKPNames());
		cbKeyPerson.setEditable(false);
		cbKeyPerson.setBounds(144, 196, 305, 20);
		contentPane.add(cbKeyPerson);
		
		JLabel lblLeadName = new JLabel("Lead navn:");
		lblLeadName.setBounds(10, 11, 124, 14);
		getContentPane().add(lblLeadName);
		
		JLabel lblLeadCompany = new JLabel("Firma:");
		lblLeadCompany.setBounds(10, 42, 124, 14);
		getContentPane().add(lblLeadCompany);
		
		JLabel lblLeadPhone = new JLabel("Telefon:");
		lblLeadPhone.setBounds(10, 73, 124, 14);
		contentPane.add(lblLeadPhone);
		
		JLabel lblLeadMail = new JLabel("E-Mail");
		lblLeadMail.setBounds(10, 106, 124, 14);
		getContentPane().add(lblLeadMail);
		
		JLabel lblLeadWebsite = new JLabel("Website:");
		lblLeadWebsite.setBounds(10, 137, 124, 14);
		getContentPane().add(lblLeadWebsite);
		
		txtLeadEmail = new JTextField();
		txtLeadEmail.setBounds(144, 103, 305, 20);
		getContentPane().add(txtLeadEmail);
		txtLeadEmail.setColumns(10);
		
		txtLeadWebsite = new JTextField();
		txtLeadWebsite.addKeyListener(new KeyListener() {
			
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		txtLeadWebsite.setBounds(144, 134, 305, 20);
		getContentPane().add(txtLeadWebsite);
		txtLeadWebsite.setColumns(10);
		
		
		
		/**
		 * Setup buttons
		 */		
		JButton btnSave = new JButton("Gem");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (createLead()) 
					closeWindow();
			}
		});
		btnSave.setBounds(261, 282, 89, 23);
		contentPane.add(btnSave);
		
		JButton btnCancel = new JButton("Annuller");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeWindow();
			}
		});
		btnCancel.setBounds(360, 282, 89, 23);
		contentPane.add(btnCancel);	
		
		lblLblerror = new JLabel("");
		lblLblerror.setForeground(Color.RED);
		lblLblerror.setBounds(10, 286, 214, 14);
		contentPane.add(lblLblerror);
		
		txtLeadPhone = new JTextField();
		txtLeadPhone.setColumns(10);
		txtLeadPhone.setBounds(144, 70, 305, 20);
		contentPane.add(txtLeadPhone);
		
		txtLeadCompany = new JTextField();
		txtLeadCompany.setColumns(10);
		txtLeadCompany.setBounds(144, 39, 305, 20);
		contentPane.add(txtLeadCompany);
		
		txtLeadName = new JTextField();
		txtLeadName.setColumns(10);
		txtLeadName.setBounds(144, 8, 305, 20);
		contentPane.add(txtLeadName);
		
		JLabel lblLeadAddedDate = new JLabel("Dato ved Oprettelse");
		lblLeadAddedDate.setBounds(10, 168, 124, 14);
		contentPane.add(lblLeadAddedDate);
		
		txtLeadAddedDate = new JTextField();
		txtLeadAddedDate.setColumns(10);
		txtLeadAddedDate.setBounds(144, 165, 305, 20);
		contentPane.add(txtLeadAddedDate);
	}
	
	private Object[] listOfKPNames()
	{
		KeyPersonCtrl kpc = new KeyPersonCtrl();
		ArrayList<KeyPerson> kpa = new ArrayList<KeyPerson>();
		
		kpa = kpc.listOfAllKeyPerson();
		return kpa.toArray();
	}
	

	private void closeWindow() 
	{
		dispose();
	}	
	
	private boolean createLead()
	{
		boolean success = false;
		LeadCtrl lc = new LeadCtrl();

		 
		 String leadCompanyName;
		 String leadPersonName;
		 String leadPhone;
		 String leadMail;
		 String leadWebsite;
		 long addedDate;
		 KeyPerson kp;
		
		try
		{
			leadCompanyName = txtLeadCompany.getText();		
			leadPersonName = txtLeadName.getText();
			leadPhone = txtLeadPhone.getText();
			leadMail = txtLeadEmail.getText();
			leadWebsite = txtLeadWebsite.getText();
			addedDate = Long.parseLong(txtLeadAddedDate.getText());
			kp = (KeyPerson) cbKeyPerson.getSelectedItem();
			
			
		
			if (!leadCompanyName.isEmpty() && !leadPersonName.isEmpty()) {
				success = true;
			} else {
				setError("Ingen data");
			}
			
			if (success) {
				lc.createLead(leadCompanyName, leadPersonName, leadPhone, leadMail, leadWebsite, addedDate);
			}
		}
		catch(NumberFormatException e)
		{
			setError("");
		}	

		
		return success;
	}

	private void setError(String err) {
		lblLblerror.setText(err);
	}
}

